package com.cxh.service.impl;

import com.cxh.base.Data2ServiceImpl;
import com.cxh.util.DataSource;
import com.cxh.dao.SysMenuDao;
import com.cxh.dao.SysRoleMenuDao;
import com.cxh.entity.SysMenu;
import com.cxh.entity.SysRoleMenu;
import com.cxh.service.SysMenuService;
import com.cxh.util.CommonUtility;
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 类说明：
 * @author  ChenXH
 * @date  2018-11-29 11:14:13
 */
@Service("sysMenuService")
@DataSource("dataSource2")
public class SysMenuServiceImpl extends Data2ServiceImpl<SysMenu> implements SysMenuService {
    @Autowired
    private SysMenuDao dao;
    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;

    @Override
    public List<SysMenu> loadPageList(Map<String, Object> map) {
       return dao.loadPageList(map);
    }
    
    @Override
    public void getMenuByUserId(String userId, HttpServletRequest request) {
    	List<SysMenu> sysMenuList= dao.getMenuByUserId(userId);
    	request.setAttribute("sysMenuList", sysMenuList);
    }
    
    @Override
    public void getMenuByRoleId(Integer id, HttpServletRequest request) {
    	//先查询所有的菜单
    	List<SysMenu> SysMenuAllList = dao.selectAll();
    	
    	//查询角色拥有的菜单
    	List<SysMenu> sysMenuList= dao.getMenuByRoleId(id);
    	
    	//将该角色有的菜单打对勾
    	ArrayList<Object> arrayList = new ArrayList<Object>();
    	
    	for (SysMenu sysMenuAll : SysMenuAllList) {
    		HashMap<String, Object> hashMap = new HashMap<String,Object>();
    		int i = 0;
    		for (SysMenu sysMenuRole : sysMenuList) {
    			if(sysMenuRole == null){
    				continue;
    			}
    			if(sysMenuAll.getId().toString().equals(sysMenuRole.getId().toString())){
    				i++;
    				hashMap.put("id", sysMenuAll.getId());
    				hashMap.put("checked", true);
    				hashMap.put("pId", sysMenuAll.getParentid()==null ? 0 : sysMenuAll.getParentid());
    				hashMap.put("name", sysMenuAll.getMenuname());
    				if(sysMenuAll.getParentid()==null){
    					hashMap.put("open", true);
    				}
    			}
    		}
    		if(i==0){
    			hashMap.put("id", sysMenuAll.getId());
				hashMap.put("pId", sysMenuAll.getParentid()==null ? 0 : sysMenuAll.getParentid());
				hashMap.put("name", sysMenuAll.getMenuname());
				if(sysMenuAll.getParentid()==null){
					hashMap.put("open", true);
				}
    		}
    		arrayList.add(hashMap);
		}
    	
    	Gson gson = new Gson();
    	String json = gson.toJson(arrayList);
    	request.setAttribute("sysMenuList", json);
    }
    
    /**
	 * @类功能说明：根据用户查询菜单信息
	 * @作者：Chenxh
	 * @创建时间：2018年12月14日下午4:17:32
	 */
    @Override
    public List<SysMenu> getMenuByUserId(String userid) {
    	return dao.getMenuByUserId(userid);
    }
    
    @Override
    public List<SysMenu> getMenu(Map<String,Object> map) {
    	return dao.getMenu(map);
    }

    @Override
    public void deleteSysMenu(SysMenu sysMenu) throws Exception {
    	//查询是否是父节点,就是查询有没有字节点
    	List<SysMenu> sysMenuChildrenList = dao.getChildren(sysMenu.getId());
    	//是父节点的话就删除包括父节点的菜单
    	if(sysMenuChildrenList.size() != 0){
    		//是父节点,删除下面所有的节点
    		for (SysMenu menu : sysMenuChildrenList) {
    			//删除的同时看下有没有角色拥有此菜单，拥有的话不能删除
    			List<SysRoleMenu> sysRoleMenuList = sysRoleMenuDao.getRoleMenuByMenuId(sysMenu.getId().toString());
    			if(sysRoleMenuList.size()!=0){
    				//有角色拥有此菜单，拥有的话不能删除
    				throw new Exception("2");
    			}
    			dao.delete(menu);
			}
    	}
    	List<SysRoleMenu> roleMenuByMenuId = sysRoleMenuDao.getRoleMenuByMenuId(sysMenu.getId().toString());
    	if(roleMenuByMenuId.size()!=0){
			//有角色拥有此菜单，拥有的话不能删除
			throw new Exception("2");
		}
    	//不是父节点的话直接删除就好了
    	dao.delete(sysMenu);
    }
    
    @Override
    public boolean getMuenLevel(String parentid) {
    	//查询一级的菜单id
    	List<SysMenu> sysMenuList = dao.getChildren(0);
    	
    	ArrayList<String> twoMenuIdList = new ArrayList<String>();
    	
    	for (SysMenu sysMenu : sysMenuList) {
    		//查询二级菜单的所有节点
    		List<SysMenu> sysMenuList2 = dao.getChildren(sysMenu.getId());
    		for (SysMenu sysMenu2 : sysMenuList2) {
    			twoMenuIdList.add(sysMenu2.getId().toString());
			}
		}
    	if(twoMenuIdList.contains(parentid)){
    		return false;
    	}
    	return true;
    }
}
